package com.beta.cust.bind.builder;

import com.beta.cat.utils.JsonUtil;
import com.beta.cust.bind.api.model.CrmCustDetailEntity;
import com.beta.cust.bind.constant.BusinessConstant;
import com.beta.cust.bind.mapper.entity.CrmCustDetail;
import com.beta.cust.bind.mapper.entity.CustInfo;
import com.beta.cust.bind.utils.PinyinUtil;
import com.beta.cust.bind.vo.CrmCustDetailVO;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: pcc
 * @Date: 2024/4/2 11:07
 * @Description:
 */
public class CustBuilder {

    public static CrmCustDetail buildCustDetail(CrmCustDetailVO vo){
        CrmCustDetail detail = new CrmCustDetail();
        detail.setCrmCustId(vo.getCstId());
        detail.setPhone(vo.getCstPhone());
        detail.setName(vo.getCstNm());
        detail.setGender(vo.getCstSex());
        detail.setEthnicGroup(vo.getNation());
        if(!CollectionUtils.isEmpty(vo.getLbls())){
            detail.setLabel(vo.getLbls().stream().collect(Collectors.joining("|")));
        }
        Map<String,String> msgMap = new HashMap<>();
        msgMap.put("dqMsg1",vo.getDqMsg1());
        msgMap.put("dqMsg2",vo.getDqMsg2());
        msgMap.put("lcMsg1",vo.getLcMsg1());
        msgMap.put("lcMsg2",vo.getLcMsg2());
        detail.setWarnNums(JsonUtil.toJson(msgMap));
        detail.setTotalAsset(BigDecimal.valueOf(vo.getAssetSum()));
        Map<String,Double> assetMap = new HashMap<>();
        if(null != vo.getAsset()){
            assetMap.put("dmdepBal",vo.getAsset().getDmdepBal());
            assetMap.put("tmdepBal",vo.getAsset().getTmdepBal());
            assetMap.put("chrtcBal",vo.getAsset().getChrtcBal());
            assetMap.put("shrAmtBal",vo.getAsset().getShrAmtBal());
        }
        detail.setAssetAllocation(JsonUtil.toJson(assetMap));
        detail.setCustManagerSubBranch(vo.getInstNm());
        detail.setCustManagerName(vo.getMgrNm());
        detail.setCustManagerPhone(vo.getMgrPhone());
        detail.setSubBranch(vo.getInstNm());
        detail.setNameFirstPinyin(PinyinUtil.getFirstChar(PinyinUtil.getPinYinFirstChar(vo.getCstNm())));
        detail.setDelFlag(BusinessConstant.IS_NOT_DEL_FLAG);
        return detail;
    }

    public static CrmCustDetailEntity buildCustDetailEntity(CrmCustDetailVO vo){
        CrmCustDetailEntity detail = new CrmCustDetailEntity();
        detail.setCrmCustId(vo.getCstId());
        detail.setPhone(vo.getCstPhone());
        detail.setName(vo.getCstNm());
        detail.setGender(vo.getCstSex());
        detail.setEthnicGroup(vo.getNation());
        if(!CollectionUtils.isEmpty(vo.getLbls())){
            detail.setLabel(vo.getLbls().stream().collect(Collectors.joining("|")));
        }
        Map<String,String> msgMap = new HashMap<>();
        msgMap.put("dqMsg1",vo.getDqMsg1());
        msgMap.put("dqMsg2",vo.getDqMsg2());
        msgMap.put("lcMsg1",vo.getLcMsg1());
        msgMap.put("lcMsg2",vo.getLcMsg2());
        detail.setWarnNums(JsonUtil.toJson(msgMap));
        detail.setTotalAsset(BigDecimal.valueOf(vo.getAssetSum()));
        Map<String,Double> assetMap = new HashMap<>();
        if(null != vo.getAsset()){
            assetMap.put("dmdepBal",vo.getAsset().getDmdepBal());
            assetMap.put("tmdepBal",vo.getAsset().getTmdepBal());
            assetMap.put("chrtcBal",vo.getAsset().getChrtcBal());
            assetMap.put("shrAmtBal",vo.getAsset().getShrAmtBal());
        }
        detail.setAssetAllocation(JsonUtil.toJson(assetMap));
        detail.setCustManagerSubBranch(vo.getInstNm());
        detail.setCustManagerName(vo.getMgrNm());
        detail.setCustManagerPhone(vo.getMgrPhone());
        detail.setSubBranch(vo.getInstNm());
        detail.setNameFirstPinyin(PinyinUtil.getFirstChar(PinyinUtil.getPinYinFirstChar(vo.getCstNm())));
        detail.setDelFlag(BusinessConstant.IS_NOT_DEL_FLAG);
        return detail;
    }

    public static CustInfo buildUpdateCustInfo(String crmCustId){
        CustInfo custInfo = new CustInfo();
        custInfo.setCertified(BusinessConstant.IS_CERTIFICATED);
        custInfo.setCrmCustId(crmCustId);
        custInfo.setUpdateTime(Timestamp.valueOf(LocalDateTime.now()));
        return custInfo;
    }
}
